<?php
namespace app\admin\controller;
use think\Controller;
use app\admin\validate;  //使用自定义验证规则  
class CategoryAd extends Controller 
{
    public function lst()
    {
		$cbRes = db('CategoryAd')->field('ca.*,c.cate_name')->alias('ca')->join('category c',"ca.category_id = c.id")->order('ca.id DESC')->paginate(4);
		$this->assign([
			'cbRes'=>$cbRes
		]);
		return view('list');
	}
	public function add()
	{
		if(request()->isPost()){
			$data = input('post.');
			
			if($data['position']=='B' || $data['position'] == 'C'){
				$cas=db('CategoryAd')->where(array('position'=>$data['position'],'category_id'=>$data['category_id']))->select();
				if($cas){
					$this->error("当前位置只能添加一个记录");
				}
			}
			if($data['link_url'] && stripos($data['link_url'],'http://') === false){
				$data['link_url']='http://'.$data['link_url'];
			}
			//验证
			// $validate = validate('categoryWords');
			// if(!$validate->check($data)){
			// 	$this->error($validate->getError());
			// }
			//处理图片上传
			if($_FILES['img_src']['tmp_name']){
				$data['img_src']=$this->upload();
			}else{
				$this->error('请上传图片');
			}
			
			$add= db('CategoryAd')->insert($data);
			if($add){
				$this->success('添加关联图片成功!','lst');
			}else{
				$this->error('添加关联图片失败');
			}
			return;
		}
	
		//获取所有的顶级分类
		$cateRes=model('Category')->where(array('pid'=>0))->select();
		$this->assign([
			'cateRes'=>$cateRes,
		]);
		return view();
	}
	public function edit($id)
	{
		//当前记录信息
		$categoryAds = db('categoryAd')->find($id);
		if(request()->isPost()){
			$data = input('post.');
			
			if($data['position']=='B' || $data['position'] == 'C'){
				$cas=db('CategoryAd')->where(array('position'=>$data['position'],'category_id'=>$data['category_id']))->select();
				if($cas){
					$this->error("当前位置只能添加一个记录");
				}
			}
			if($data['link_url'] && stripos($data['link_url'],'http://') === false){
				$data['link_url']='http://'.$data['link_url'];
			}
			// $validate = validate('categoryBrands');
			// if(!$validate->check($data)){
			// 	$this->error($validate->getError());
			// }
			//处理图片上传
			if($_FILES['img_src']['tmp_name']){
				if($categoryAds['img_src']){
					$cbImg = IMG_UPLOADS.DS.$categoryAds['img_src'];
						
						if(file_exists($cbImg)){
							//删除指定文件的路径
							@unlink($cbImg);
						}
				}
				$data['img_src']=$this->upload();
			}else{
				$this->error('请上传图片');
			}
			
			
			$save= db('CategoryAd')->update($data);
			if($save !== false){
				$this->success('修改关联图片成功!','lst');
			}else{
				$this->error('修改关联图片失败');
			}
			return;
		}
		$cateRes=model('Category')->where(array('pid'=>0))->select();
		$this->assign([
			'cateRes' => $cateRes,
			'categoryAds'=>$categoryAds
		]);
		return view();
	}
	public function del($id)
	{
		$cbObj=db('categoryAd');
		$cbs = $cbObj->field('img_src')->find($id);
		if($cbs['img_src']){
			$cbImg = IMG_UPLOADS.DS.$cbs['img_src'];
				
				if(file_exists($cbImg)){
					//删除指定文件的路径
					@unlink($cbImg);
				}
		}
		$del = $cbObj->delete($id);
		if($del){
			$this->success('删除关联图成功','lst');
		}else{
			$this->error('删除关联图失败');
		}
	}
	public function upload(){
		    // 获取表单上传文件 例如上传了001.jpg
		    $file = request()->file('img_src');
		    
		    // 移动到框架应用根目录/public/uploads/ 目录下
		    if($file){
		        $info = $file->move(ROOT_PATH . 'public' .DS.'static'. DS . 'uploads');
		        if($info){
		            return $info->getSaveName();
		        }else{
		            // 上传失败获取错误信息
		            echo $file->getError();
					die;
		        }
		    }
		}
	
}
